<?php

namespace app\api\controller\user;

use app\api\controller\AuthController;
use app\api\service\UserAddressService;
use think\App;
use think\exception\ValidateException;
use think\facade\Db;

class AddressController extends AuthController
{
 
    /**
     * 地址标签
     */
    public function lableList()
    {
        $param = $this->request->param();
        if (empty($param['store_id'])){
            return  json(['code' =>0, 'msg' => '非法参数']);
        }
        $user_id = $this->request->user['user_id'];
        $list = Db::name('user_address_label')->where('uid', $user_id)
        ->where('delete_time', 0)
        ->where('store_id', $param['store_id'])
        ->where('status', 1)->select();
        return  json(['code' =>1, 'msg' => '查询成功', 'data'=>$list]);
    }

    /**
     * 删除标签
     */
    public function delLabel()
    {
        $param = $this->request->param();
        if (empty($param['store_id'])){
            return  json(['code' =>0, 'msg' => '非法参数']);
        }
        if (empty($param['id'])){
            return  json(['code' =>0, 'msg' => '请选择要删除的标签']);
        }
        $user_id = $this->request->user['user_id'];
        $param['uid'] = $user_id;
        $rst = UserAddressService::delLabel($param);
        if ($rst) {
            return  json(['code' =>1, 'msg' => '删除成功', 'data'=>'']);
        } else{
            return  json(['code' =>0, 'msg' => '删除失败', 'data'=>'']);
        }
    }

    /**
     * 增加标签
     */
    public function addLabel()
    {
        $param = $this->request->param();
        if (empty($param['store_id'])){
            return  json(['code' =>0, 'msg' => '非法参数']);
        }
        if (empty($param['name'])){
            return  json(['code' =>0, 'msg' => '请输入标签名称']);
        }
        $user_id = $this->request->user['user_id'];
        $param['uid'] = $user_id;
        $rst = UserAddressService::addLabel($param);
        if ($rst) {
            return  json(['code' =>1, 'msg' => '添加成功', 'data'=>'']);
        } else{
            return  json(['code' =>0, 'msg' => '添加失败', 'data'=>'']);
        }
       
    }

   public function index()
   {
        $addresses = UserAddressService::getUserAddressList($this->request->user['user_id']);

        return json(['code' => 1, 'data' => ['addresses' => $addresses], 'msg' => '请求成功!']);
   }

   public function read()
   {
       $address_id = $this->request->param('address_id/d');

       if (empty($address_id)){
           return  json(['code' =>0, 'msg' => '非法参数']);
       }


       $address = UserAddressService::findUserAddress($address_id);

       return json(['code' => 1, 'data' => ['address' => $address], 'msg' => '请求成功!']);
   }

   public function save()
   {
        $param = $this->request->param();
        $label = empty($param['label'])?'':$param['label'];
        try{
            validate('UserAddressValidate')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0,'msg' => $e->getError()]);
        }

        $param['user_id'] = $this->request->user['user_id'];
        $user_id = $this->request->user['user_id'];
        $result = UserAddressService::saveUserAddress($param, $label, $user_id);

        return $result ? json(['code' => 1, 'msg' => '添加地址成功!']) : json(['code' => 0, 'msg' => '添加地址失败!']);
   }

    public function update()
    {
        $param = $this->request->param();
        $label = empty($param['label'])?'':$param['label'];
        try{
            validate('UserAddressValidate.edit')->check($param);
        }catch (ValidateException $e){
            return json(['code' => 0,'msg' => $e->getError()]);
        }
        $user_id = $this->request->user['user_id'];
        UserAddressService::updateUserAddress($param, $label, $user_id);

        return  json(['code' => 1, 'msg' => '地址更新成功!']);
    }

    public function delete()
    {
        $address_id = $this->request->param('address_id/d');

        if (empty($address_id)){
            return  json(['code' =>0, 'msg' => '非法参数']);
        }

        $result = UserAddressService::deleteUserAddress($address_id);

        return $result ? json(['code' => 1, 'msg' => '地址删除成功!']) : json(['code' => 0, 'msg' => '地址删除失败!']);
    }
}